package com.easidea.tim.flex.remote.inventory
{
	import com.easidea.tim.flex.Env;
	import com.easidea.tim.flex.bean.Page;
	import com.easidea.tim.flex.bean.inventory.Inventory;
	import com.easidea.tim.flex.event.CustomerEvent;
	import com.easidea.tim.flex.remote.BaseDao;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import net.digitalprimates.persistence.hibernate.rpc.HibernateRemoteObject;
	

	public class InventoryDao extends BaseDao
	{
		private var _service:HibernateRemoteObject = new HibernateRemoteObject();
		private static var _instance:InventoryDao;
		[Bindable]private var getDetailType:String = null;
		[Bindable]private var statusType:String = null;
		
		/**
		 * Singleton
		 */
		public static function getInstance() : InventoryDao {
			if ( _instance == null ){
				_instance = new InventoryDao();
			}
			return _instance;
		}
		
		function InventoryDao(){
			_service.channelSet = this.buildChannelSet();
			_service.destination = "inventoryService";
			_service.concurrency = "last";
			_service.makeObjectsBindable = true;
			_service.showBusyCursor = true;
		}
		
		public function save(ti:Inventory):void{
			_service.addEventListener(ResultEvent.RESULT, processSave);
			_service.addEventListener(FaultEvent.FAULT,_Fault);
			_service.saveInventory(ti);
		}
		
		private function processSave(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT, processSave);
			Env.dispatchEvent(new CustomerEvent("processSaveCallBack",e.result as Boolean));
		}
		
		
		public function getInventoryList(name:String,colorCode:String, sampleFlag:Boolean, operator:String, startDate:Date, endDate:Date, status:String, page:Page):void{
			_service.addEventListener(ResultEvent.RESULT, processGetInventoryList);
			_service.addEventListener(FaultEvent.FAULT,_Fault);
			_service.getInventoryList(name,colorCode,sampleFlag,operator,startDate,endDate,status, page);
		}
		
		private function processGetInventoryList(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT,processGetInventoryList);
			if(e.result != null){
				var page:Page = e.result as Page;
				Env.dispatchEvent(new CustomerEvent("getInventoryListSuccess",page));
			}
		}
		
		public function getInventoryDetail(id:Number, getDetailType:String):void{
			_service.addEventListener(ResultEvent.RESULT, processGetInventoryDetail);
			_service.addEventListener(FaultEvent.FAULT,_Fault);
			this.getDetailType = getDetailType;
			_service.getInventoryDetail(id,"detail");
		}
		
		private function processGetInventoryDetail(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT, processGetInventoryDetail);
			if(e.result != null){
				Env.dispatchEvent(new CustomerEvent(this.getDetailType,e.result));
			}
		}
		
		public function getInventoryMarginDetail(id:Number):void{
			_service.addEventListener(ResultEvent.RESULT, processGetInventoryMarginDetail);
			_service.addEventListener(FaultEvent.FAULT,_Fault);
			_service.getInventoryDetail(id,"margin");
		}
		
		private function processGetInventoryMarginDetail(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT, processGetInventoryDetail);
			if(e.result != null){
				Env.dispatchEvent(new CustomerEvent("getInventoryMarginDetailSuccess",e.result));
			}
		}
		
		public function updateItem(name:String,colorCode:String,sampleFlag:Boolean,index:int,value:Number,inventoryDate:Date):void{
			_service.addEventListener(ResultEvent.RESULT, processUpdateItem);
			_service.addEventListener(FaultEvent.FAULT,_Fault);
			_service.updateItemMeterNum(name,colorCode,sampleFlag,index,value,inventoryDate);
		}
		
		private function processUpdateItem(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT, processUpdateItem);
			Env.dispatchEvent(new CustomerEvent("updateItemSuccess",e.result as Boolean));
		}
		
		
		public function removeDust(id:Number):void{
			_service.addEventListener(ResultEvent.RESULT, processRemoveDust);
			_service.addEventListener(FaultEvent.FAULT,_Fault);
			_service.removeDust(id);
		}

		private function processRemoveDust(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT,processRemoveDust);
			Env.dispatchEvent(new CustomerEvent("removeDustSuccess",e.result));
		}
		
		public function inputFinished(id:Number):void{
			_service.addEventListener(ResultEvent.RESULT, processInputFinished);
			_service.addEventListener(FaultEvent.FAULT,_Fault);
			_service.inputFinished(id);
		}
		
		private function processInputFinished(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT, processInputFinished);
			Env.dispatchEvent(new CustomerEvent("inputFinishedSuccess",e.result));
		}
		
		public function changeStatus(id:Number, type:String):void{
			_service.addEventListener(ResultEvent.RESULT, processChangeStatus);
			_service.addEventListener(FaultEvent.FAULT,_Fault);
			statusType = type;
			_service.changeStatus(id,type);
		}
		
		private function processChangeStatus(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT, processChangeStatus);
			Env.dispatchEvent(new CustomerEvent(this.statusType,e.result));
		}
		
		public function updateInventory(upList:ArrayCollection,inventoryId:int):void{
			_service.addEventListener(ResultEvent.RESULT, processUpdateInventory);
			_service.addEventListener(FaultEvent.FAULT,_Fault);
			_service.updateInventory(upList,inventoryId);
		}
		
		private function processUpdateInventory(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT, processUpdateInventory);
			Env.dispatchEvent(new CustomerEvent("processSaveCallBack",e.result));
		}
		
		public function getMarginListData(id:int):void {
			//getMarginListDataSuccess
			_service.addEventListener(ResultEvent.RESULT ,processGetMarginListData);
			_service.addEventListener(FaultEvent.FAULT,_Fault);
			_service.getMarginListData(id);
		}

		private function processGetMarginListData(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT, processGetMarginListData);
			Env.dispatchEvent(new CustomerEvent("getMarginListDataSuccess",e.result));
		}
		
		public function deleteInv(id:int):void{
			_service.addEventListener(ResultEvent.RESULT ,processDeleteInv);
			_service.addEventListener(FaultEvent.FAULT,_Fault);
			_service.deleteInv(id);
		}
		
		private function processDeleteInv(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT, processDeleteInv);
			Env.dispatchEvent(new CustomerEvent("deleteInvSuccess",e.result));
		}
		
	}
}